//
//  Database.swift
//  ShareMusic
//
//  Created by 胡岩 on 16/5/10.
//  Copyright © 2016年 胡岩. All rights reserved.
//

import UIKit
import FMDB

enum DBType {
    case Collect
    case Download
}
class Database: NSObject {
    var db:FMDatabase?
    override init() {
        super.init()
        self.open()
        self.createTable()
    }
    
    func open() {
        db = FMDatabase.init(path: (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent( "database.sqlite"))
    }
    
    func createTable() {
        if db?.open() == true {
            do {
                try db?.executeUpdate("create table if not exists local (id integer primary key autoincrement, songid NSNumber, songname text, singerid NSNumber, url text, type text, singername text)", values: nil)
            } catch {
                
            }
        }
        db?.close()
    }
    
    func insert(model:songModel?, type:DBType) {
        let dbType:DBType = type == DBType.Collect ? DBType.Collect : DBType.Download
        if db?.open() == true {
            do {
                try db?.executeUpdate("insert into local(songid, songname, singerid, url, type, singername) values('\((model?.song_id)!)', '\((model?.song_name)!)', '\((model?.singer_id)!)', '\((model?.songDetails?.url)!)', '\(dbType)', '\((model?.singer_name)!)')", values: nil)
            } catch {
                
            }
        }
        db?.close()
    }
    
    func delete(model:songModel?, type:DBType) {
        let dbType:DBType = type == DBType.Collect ? DBType.Collect : DBType.Download
        if db?.open() == true {
            do {
                try db?.executeUpdate("delete from local where songid = '\((model?.song_id)!)' and type = '\(dbType)'", values: nil)
            } catch {
                
            }
        }
        db?.close()
    }
    
    
    func haveData(model:songModel?, type:DBType) -> Bool {
        let dbType:DBType = type == DBType.Collect ? DBType.Collect : DBType.Download
        let result:FMResultSet?
        if db?.open() == true {
            do {
                try result = db?.executeQuery("select * from local where type = '\(dbType)'", values: nil)
                while result?.next() == true {
                    if "\((model?.song_id)!)" == result?.stringForColumn("songid") {
                        db?.close()
                        return true
                    }
                }
            } catch {
                
            }
        }
        db?.close()
        return false
    }
    
    
    func select(type:DBType) -> NSArray {
        let dbType:DBType = type == DBType.Collect ? DBType.Collect : DBType.Download
        let result:FMResultSet?
        let array:NSMutableArray = NSMutableArray.init(capacity: 0)
        if db?.open() == true {
            do {
                try result = db?.executeQuery("select * from local where type = '\(dbType)'", values: nil)
                while result?.next() == true {
                    let model:songModel = songModel.init()
                    model.song_id = Int((result?.intForColumn("songid"))!)
                    model.singer_id = Int((result?.intForColumn("singerid"))!)
                    model.song_name = result?.stringForColumn("songname")
                    model.songDetails?.url = result?.stringForColumn("url")
                    model.singer_name = result?.stringForColumn("singername")
                    if dbType == DBType.Download {
                        model.type = 2
                    }
                    array.addObject(model)
                }
            } catch {
                
            }
        }
        return array
    }
    
    
    
}
